package com.abner.springcloud.consumer.movie.controller;

import com.abner.springcloud.consumer.movie.engity.User;
import com.abner.springcloud.consumer.movie.service.UserFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@Slf4j
public class MovieController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable String id){
        log.info("[{}] -- start query user information , id : [{}]",this.getClass(),id);
        User user = null;
        try {
            user = userFeignClient.getUserById(id);
        }catch (Exception e){
            log.error("[{}],query user is error , exception is : [{}]",this.getClass(),e);
        }
        return user;
    }

    @GetMapping("/getUser")
    public User getUser(@RequestParam("id") String id,@RequestParam("username") String username){
        log.info("[{}] -- start query user information , id : [{}],username:[{}]",this.getClass(),id,username);
        User user = null;
        try {
            user = userFeignClient.getUserByIdAndUsername(id,username);
        }catch (Exception e){
            log.error("[{}],query user is error , exception is : [{}]",this.getClass(),e);
        }
        return user;
    }

    @GetMapping("/getUserByMap")
    public User getUserByMap(@RequestParam Map<String,String> map){
        log.info("[{}] -- start query user information , map : [{}]",this.getClass(),map);
        User user = null;
        try {
            user = userFeignClient.getUserByIdAndUsername(map.get("id"),map.get("username"));
        }catch (Exception e){
            log.error("[{}],query user is error , exception is : [{}]",this.getClass(),e);
        }
        return user;
    }

    @PostMapping("/add")
    public String getUser(@RequestBody User user){
        log.info("[{}] -- start add user information , user : [{}]",this.getClass(),user);
        String result = null;
        try {
            result = userFeignClient.addUser(user);
        }catch (Exception e){
            result = "error";
            log.error("[{}],add user:[{}] is error , exception is : [{}]",this.getClass(),user,e);
        }
        return result;
    }
}
